'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
const $ = dbCmd.aggregate;
exports.main = async (event, context) => {
	let rechargeDatas = await db.collection('wmz-entitlement-order').where({
		status: "已退款" ,
		coupon_status: dbCmd.exists(false)
	}).limit(100).orderBy('create_time', 'desc').get()
	rechargeDatas=rechargeDatas.data
	console.log(rechargeDatas.length)
	for(var i in rechargeDatas){
		console.log(rechargeDatas[i])
		await db.collection('wmz-entitlement-coupon').where({
			order_id:rechargeDatas[i]._id
		}).update({
			status:'已使用'
		}) 
		await db.collection('wmz-entitlement-order').doc(rechargeDatas[i]._id).update({
			coupon_status:true
		})
	}
	return event
};
